<?php

	class collocate_goods_modifiers extends pageFactory
	{
		/* 后台商品添加 */
		function add(&$content)
		{
			$find = array(
				'<span>相关商品</span></li>',
				'</form>',
				'</head>'
			);
			$replace[] = $find[0].'<li id="collocation"><span>搭配商品</span></li>';
			$replace[] = '
			<div class="spage-main-boxs">
			<div id="collotion_block"></div>
			</div>'.$find[1];
			$replace[] = '<script>
				<!--
				window.addEvent("domready",function(){
					$("collocation").addEvents({
					"click":function(e){
						var post_data = $("gEditor").toQueryString();
						W.page("index.php?ctl=plugins/ctl_collocate_goods&act=init",
						{method:"post",update:"collotion_block",data:post_data});
						this.fireEvent("show");
					},
					"show":function(){
						$("collotion_block").getParent(".spage-main-boxs").show();
						this.addClass("cur");
						$ES(".spage-main-box").each(function(item){ if(!item.hasClass("spage-app-box")) item.hide();});
						$ES(".spage-side-nav .l-handle").each(function(item){item.removeClass("cur");});
					},
					"hide":function(item){
						if(item) 
						if(item.hasClass("all")){
							item.addClass("cur");
							$ES("#gEditor-Body .spage-main-box").each(function(item){item.show();});
						}
						$("collotion_block").getParent(".spage-main-boxs").hide();
						if(this.hasClass("cur"))
						this.removeClass("cur");
					}
					});
				});
				$("collocation").fireEvent("hide");
				$ES(".spage-side-nav .l-handle").each(function(item){
					if(item)
					item.addEvent("click",function(){                               
						$("collocation").fireEvent("hide",this);
					});
				});
				//-->
			</script>
			</head>';
			$content = str_replace($find,$replace,$content);
			return $content;
		}
		
		/* 后台商品编辑 */
		function edit(&$content)
		{
			return $content = $this->add($content);
		}
		
		/* 前台商品详细页 */
		function product_index(&$content)
		{
			$objCollocation = $this->system->loadModel('plugins/collocate_goods/collocGoods');

			$this->action = $this->system->request['action'];
			$objGoods = $this->system->loadModel('trading/goods');
			//取得搭配商品配置信息
			$gid = $this->action['args']['0'];
			$objPdtFinder = &$this->system->loadModel('goods/finderPdt');
			$aData = $objGoods->getGoodsMemo($gid,'collocation');
			$aCollocation = unserialize($aData);
			foreach ($aCollocation as $k => $v)
			{
				if ('filter' == $v['type'])
				{
					if($rows['set_price'] == 'minus')
					{
						$cols = 'goods_id,price-'.intval($v['price']).' AS colloprice';
					}else
					{
						$cols = 'goods_id,price*'.($v['price']?$v['price']:1).' AS colloprice';
					}
					parse_str($v['items'].'&dis_goods[]='.$gid, $arr);
					//取得过滤条件的结果
					$aFilterGoodsList = $objPdtFinder->getList($cols,$arr,0,-1);
					foreach ($aFilterGoodsList as $list)
					{
						//取出结果中的goods_id
						$aTmp[] = $list['goods_id'];
					}
					$filterGoodsList = array_unique($aTmp);
					$aCollocation[$k]['items'] = $filterGoodsList;
				}
				//取得搭配商品详细
				foreach ($aCollocation[$k]['items'] as $key => $vGoods)
				{
					$aCollocation[$k]['items'][$vGoods] = $objCollocation->getCollocItemsDetail($vGoods);
					unset($aCollocation[$k]['items'][$key]);
				}
			}
			//error_log(print_r($aCollocation,true),3,__FILE__.'.'.__FUNCTION__.'.aCollocation.log');//alone add
			$collcationHtml = $this->_fetch_compile_include( dirname(__FILE__).'/view/product/collocation.html',array('collocation'=>$aCollocation));

			$find = array('<div class="goods-detail-tab clearfix">');
			$replace[] = $collcationHtml.$find['0'];
			$content = str_replace($find,$replace,$content);
			return $content;
		}
		
		/* 前台mini购物车 */
		function cart_addGoodsToCart(&$content)
		{
			//error_log(print_r($content,true),3,__FILE__.'.'.__FUNCTION__.'.content.log');//alone add
			return $content;
		}
		/* 前台购物车页面 */
		function cart_index(&$content)
		{
			//error_log(print_r($content,true),3,__FILE__.'.'.__FUNCTION__.'.content.log');//alone add
			preg_match_all('/\<tr[^\>]*\s+[^\<]*urlupdate="[\?]?cart-g-(\d+)@[^\s]*"\s+[^\<]*urlremove="([^\s]*)"\s+[^\<]*\>/is',$content,$match);
			//error_log(print_r($match,true),3,__FILE__.'.'.__FUNCTION__.'.match.log');//alone add
			foreach ($match[0] as $k=>$v)
			{
				$tmp = str_replace('>',' p_id=0 g_id='.$match[1][$k].'>',$v);
				$replace[] = str_replace('cart','action_cart',$tmp);
			}
			//error_log(print_r($replace,true),3,__FILE__.'.'.__FUNCTION__.'.replace.log');//alone add
			$content = str_replace($match[0],$replace,$content);
			return $content;
		}
		/* 前台更新购物车 */
		function cart_updateCart(&$content)
		{
			//error_log(print_r($content,true),3,__FILE__.'.'.__FUNCTION__.'.content.log');//alone add
			return $content;
		}
		/* 前台购物信息 */
		function cart_checkout(&$content)
		{
			//error_log(print_r($content,true),3,__FILE__.'.'.__FUNCTION__.'.content.log');//alone add
			return $content;
		}
	}
?>